﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using ERP_RORZE_SYSTEM;
using System.Data.OleDb;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ERP_RORZE_DLL.Business
{
    public partial class panel_Yoyaku : UserControl
    {
        private INISettings mySetting;
        private SqlConnection myConnection;        
        public panel_Yoyaku()
        {
            InitializeComponent();
            mySetting = new INISettings();
            myConnection = new SqlConnection(mySetting.SQLConnectionString);
            
            LoadData();            

            this.Yoyaku.PopupMenuShowing += Yoyaku_PopupMenuShowing;
            this.yoyakuContext.ItemClicked += yoyakuContext_ItemClicked;
        }

        void yoyakuContext_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            this.yoyakuContext.Hide();
            switch (e.ClickedItem.Name)
            {
                case "forecastSelection":
                    frm_ForecastPlan_Yoyaku forecast_Form = new frm_ForecastPlan_Yoyaku();
                    forecast_Form.ShowDialog();
                    LoadData();
                    break;
                case "refreshSelection":
                    LoadData();
                    break;
            }
        }

        void Yoyaku_PopupMenuShowing(object sender, DevExpress.XtraGrid.Views.Grid.PopupMenuShowingEventArgs e)
        {
            this.yoyakuContext.Show(MousePosition);
        }

        private void LoadData()
        {
            DataSet sourceDataSet = new DataSet();            
            SQLManagement mySQLManagement = new SQLManagement();
            mySQLManagement.SQLConnectionString = mySetting.SQLConnectionString;
            sourceDataSet = mySQLManagement.SQL_Select("[241_Yoyaku]", "");
            if (sourceDataSet != null)
            {
                gridYoyaku.DataSource = sourceDataSet.Tables[0];
            }
            else
            {
                MessageBox.Show("There is no data for Yoyaku");
                DataTable newTable = new DataTable();
                newTable.Columns.Add("ID", typeof(string));
                newTable.Columns.Add("PlanNo", typeof(string));
                newTable.Columns.Add("RobotNo", typeof(string));
                newTable.Columns.Add("RobotQuantity", typeof(int));
                newTable.Columns.Add("RobotPartDate", typeof(DateTime));
                newTable.Columns.Add("PartNo", typeof(string));
                newTable.Columns.Add("NeedQuantity", typeof(int));
                newTable.Columns.Add("ImportOrderNo", typeof(string));
                newTable.Columns.Add("RRCDeadLine", typeof(DateTime));
                newTable.Columns.Add("ArrivalDeadLine", typeof(DateTime));
                newTable.Columns.Add("SupplyQuantity", typeof(int));
                gridYoyaku.DataSource = newTable;
            }
            Yoyaku.Columns[0].Caption = "ID";
            Yoyaku.Columns[1].Caption = "Plan No";
            Yoyaku.Columns[2].Caption = "Robot No";
            Yoyaku.Columns[3].Caption = "Robot Quantity";
            Yoyaku.Columns[4].Caption = "Robot Part Date";
            Yoyaku.Columns[5].Caption = "Part No";
            Yoyaku.Columns[6].Caption = "Need Quantity";
            Yoyaku.Columns[7].Caption = "Import Order No";
            Yoyaku.Columns[8].Caption = "RRC DeadLine";
            Yoyaku.Columns[9].Caption = "Arrival DeadLine";
            Yoyaku.Columns[10].Caption = "Supply Quantity";
            Yoyaku.Columns[0].Visible = false;
            Yoyaku.BestFitColumns();
        }

        private void cleranceAdd_Click(object sender, EventArgs e)
        {
            frm_ForecastPlan_Yoyaku forecast_Form = new frm_ForecastPlan_Yoyaku();
            forecast_Form.ShowDialog();
            LoadData();
        }

        private void buttonRefresh_Click(object sender, EventArgs e)
        {
            LoadData();
        }
    }
}
